<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="加注时间" prop="fillDate">
        <el-date-picker clearable
          v-model="queryParams.fillDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择加注时间">
        </el-date-picker>
      </el-form-item> -->
    
       <el-form-item label="加注时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="车牌" prop="licenseNum">
        <el-input
          v-model="queryParams.licenseNum"
          placeholder="请输入车牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <el-form-item label="标号" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="标号" clearable>
          <el-option
            v-for="dict in dict.type.t_oil_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="容器状态" prop="state">
        <el-select v-model="queryParams.fullStatus" placeholder="容器状态" clearable>
          <el-option
            v-for="dict in dict.type.t_full_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="油站名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入油站名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ljj:gasoline:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ljj:gasoline:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ljj:gasoline:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ljj:gasoline:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="getInfo()"
          type="warning"
          plain
          icon="el-icon-s-data"
          size="mini"
          v-hasPermi="['ljj:gasoline:list']"
        >实时信息</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gasolineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="加注时间" align="center" prop="fillDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fillDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="grade" label="标号" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_oil_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="总金额" align="center" prop="totalFee" />
      <el-table-column label="优惠单价" align="center" prop="discountsPrice" />
      <el-table-column label="优惠实付金额" align="center" prop="discountsTotalFee" />
      <el-table-column label="每升优惠金额" align="center" prop="discountsIntervalPrice" />
      <el-table-column label="优惠总金额" align="center" prop="discountsIntervalTotal" />
      <el-table-column prop="fullStatus" label="是否加满" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_full_status" :value="scope.row.fullStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="对比上次加注时间" align="center" prop="kmBeforeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.kmBeforeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="距上次加注间隔时间" align="center" prop="fillIntervalTime" />
      <el-table-column label="当前里程" align="center" prop="km" />
      <el-table-column label="距上次公里数" align="center" prop="kmInterval" />
      <el-table-column label="百公里油耗" align="center" prop="kmConsume" />
      <el-table-column label="每公里金额" align="center" prop="kmPrice" />
      <el-table-column label="优惠后每公里金额" align="center" prop="discountsKmPrice" />
      <el-table-column label="车牌" align="center" prop="licenseNum" />
      <el-table-column label="油站名称" align="center" prop="stationName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:gasoline:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:gasoline:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加油对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="加注时间" prop="fillDate">
          <el-date-picker clearable
            v-model="form.fillDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择加注时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
             <span slot="label">
               <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
              <i class="el-icon-question"></i>
               </el-tooltip>
              标号
            </span>
            <el-radio-group v-model="form.grade">
               <el-radio
                v-for="dict in dict.type.t_oil_grade"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item>
             <span slot="label">
               <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
              <i class="el-icon-question"></i>
               </el-tooltip>
              是否注满
            </span>
            <el-radio-group v-model="form.fullStatus">
               <el-radio
                v-for="dict in dict.type.t_full_status"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="优惠实付金额" prop="discountsTotalFee">
          <el-input v-model="form.discountsTotalFee" placeholder="请输入优惠实付金额" />
        </el-form-item>
        <el-form-item label="当前里程" prop="km">
          <el-input v-model="form.km" placeholder="请输入当前里程" />
        </el-form-item>
        <el-form-item label="车牌" prop="licenseNum">
          <el-input v-model="form.licenseNum" placeholder="请输入车牌" />
        </el-form-item>
        <el-form-item label="油站名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入油站名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGasoline, getGasoline, delGasoline, addGasoline, updateGasoline } from "@/api/ljj/gasoline/gasoline";

export default {
  name: "Gasoline",
  dicts: ['t_full_status', 't_oil_grade'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加油表格数据
      gasolineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fillDate: null,
        price: null,
        count: null,
        totalFee: null,
        discountsPrice: null,
        discountsTotalFee: null,
        discountsIntervalPrice: null,
        discountsIntervalTotal: null,
        fullStatus: null,
        kmBeforeTime: null,
        fillIntervalTime: null,
        km: null,
        kmInterval: null,
        kmConsume: null,
        kmPrice: null,
        discountsKmPrice: null,
        licenseNum: null,
        stationName: null,
        grade: null,
      },
       // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 实时详情
    getInfo(){
      this.$router.push('/ljj/task-getInfoV/getInfoV')
    },
    /** 查询加油列表 */
    getList() {
      this.loading = true;
      listGasoline(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.gasolineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fillDate: null,
        price: null,
        count: null,
        totalFee: null,
        discountsPrice: null,
        discountsTotalFee: null,
        discountsIntervalPrice: null,
        discountsIntervalTotal: null,
        fullStatus: "0",
        grade: "0",
        kmBeforeTime: null,
        fillIntervalTime: null,
        km: null,
        kmInterval: null,
        kmConsume: null,
        kmPrice: null,
        licenseNum: null,
        stationName: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGasoline(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGasoline(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGasoline(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //初始化时间
    createDate() {
      var nowDate = new Date(new Date().getTime()-1000*60*60*24*30);
      var nowDate2 = new Date(new Date().getTime()+1000*60*60*24);
      var startdate = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        day: nowDate.getDate(),
      }
        var endDate = {
        year: nowDate2.getFullYear(),
        month: nowDate2.getMonth() + 1,
        day: nowDate2.getDate(),
      }
      var startdate = startdate.year + '-' + (startdate.month >= 10 ? startdate.month : '0' + startdate.month) + '-' + (startdate.day >= 10 ? startdate.day : '0' + startdate.day);
      var endDate = endDate.year + '-' + (endDate.month >= 10 ? endDate.month : '0' + endDate.month) + '-' + (endDate.day >= 10 ? endDate.day : '0' + endDate.day);
      this.dateRange = [startdate,endDate];
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除加油编号为"' + ids + '"的数据项？').then(function() {
        return delGasoline(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/gasoline/export', {
        ...this.queryParams
      }, `gasoline_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
